package org.example.userservice.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.context.BaseContext;
import org.example.mapper.CartMapper;
import org.example.mapper.CustomerMapper;
import org.example.mapper.GoodsMapper;
import org.example.mapper.SalelogMapper;
import org.example.pojo.entity.Cart;
import org.example.pojo.userdto.CartBeanDTO;
import org.example.pojo.entity.Customer;
import org.example.pojo.entity.Goods;
import org.example.pojo.entity.SaleLog;
import org.example.userservice.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SalelogMapper salelogMapper;
    @Override
    public boolean addShoppingCart(CartBeanDTO cartBeanDTO) {
        Cart cart = new Cart();
        SaleLog saleLog = new SaleLog();
        cart.setGoodid(cartBeanDTO.getGoodId());saleLog.setGoodid(cartBeanDTO.getGoodId());
        Goods goods = goodsMapper.findById(cartBeanDTO.getGoodId());
        if(goods.getInventory()==goods.getSalenum()){
            return false;
        }
        Long userid = BaseContext.getCurrentId();Customer customer = customerMapper.getCustomerById(userid);
        cart.setUserid(userid);saleLog.setUserid(userid);
        List<Cart> listcart = cartMapper.list(cart);
        if(listcart != null && listcart.size() > 0) {
            Cart cart1 = listcart.get(0);
            cart1.setNum(cart1.getNum() + 1);
            cartMapper.update(cart1);
        }else{
            cart.setNum(1);
            cart.setGoodname(goods.getName());
            cart.setPrice(goods.getPrice());
            cart.setCreatetime(LocalDateTime.now());
            cart.setUsername(customer.getUser());
            cartMapper.insert(cart);
        }
        saleLog.setNum(1);
        saleLog.setGoodname(goods.getName());
        saleLog.setPrice(goods.getPrice());
        saleLog.setSaletime(LocalDateTime.now());
        saleLog.setUsername(customer.getUser());
        salelogMapper.insertSaleLog(saleLog);
        goods.setSalenum(goods.getSalenum()+1);
        goodsMapper.sell(goods);
        return true;
    }

    public List<Cart> ShoppingCartlist() {
        Long userid = BaseContext.getCurrentId();
        Cart cart = new Cart();
        cart.setUserid(userid);
        return cartMapper.list(cart);
    }

    @Override
    public void deleteShoppingCart() {
        Long userid = BaseContext.getCurrentId();
        Cart cart = new Cart();
        cart.setUserid(userid);
        List<Cart> listcart = cartMapper.list(cart);
        for(int i=0;i<listcart.size();i++){
            Goods goods = goodsMapper.findById(listcart.get(i).getGoodid());
            goods.setSalenum(goods.getSalenum()-listcart.get(i).getNum());
            goodsMapper.cancel(goods);
        }
        cartMapper.deletebyid(userid);
    }


}
